import request from "@/utils/request";
import {
  BaseSaleAttrResponseData,
  SkuData,
  SKUResponseData,
  SpuDetail,
  SpuImageResponseData,
  SpuResponseData,
  SpuSaleAttrResponseData,
  TrademarkResponseData,
} from "@/api/product/spu/type.ts";

enum API {
  // GET /admin/product/{page}/{limit} 获取SPU分页列表
  SPU_LIST_URL = "/admin/product/",
  // GET /admin/product/baseTrademark/getTrademarkList 获取SPU所有的品牌列表
  TRADEMARK_LIST_URL = "/admin/product/baseTrademark/getTrademarkList",
  // /admin/product/spuImageList/{spuId} 获取某一个spu的图片列表
  SPU_IMAGE_LIST_URL = "/admin/product/spuImageList/",
  // /admin/product/spuSaleAttrList/{spuId} 获取某一个spu的销售属性列表
  SPU_SALE_ATTR_LIST_URL = "/admin/product/spuSaleAttrList/",
  // /admin/product/baseSaleAttrList 获取销售属性列表
  BASE_SALE_ATTR_LIST_URL = "/admin/product/baseSaleAttrList",
  // /admin/product/saveSpuInfo 添加SPU
  SAVE_SPU_INFO_URL = "/admin/product/saveSpuInfo",
  // /admin/product/updateSpuInfo 修改SPU
  UPDATE_SPU_INFO_URL = "/admin/product/updateSpuInfo",
  // /admin/product/saveSkuInfo post 保存SKU
  SAVE_SKU_INFO_URL = "/admin/product/saveSkuInfo",
  //   GET /admin/product/findBySpuId/{spuId} 根据spuId获取spu的基本信息
  SKU_INFO_URL = "/admin/product/findBySpuId/",
  // 删除 /admin/product/deleteSpu/{spuId} delete 根据spuId删除对应的spu
  DELETE_SKU_URL = "/admin/product/deleteSpu/",
}

// 获取三级分类id下的商品列表
export const reqSpuList = (
  page: number,
  limit: number,
  category3Id: number | string,
) =>
  request.get<any, SpuResponseData>(
    API.SPU_LIST_URL + `${page}/${limit}?category3Id=${category3Id}`,
  );

// 获取SPU所有的品牌列表
export const reqTrademarkList = () =>
  request.get<any, TrademarkResponseData>(API.TRADEMARK_LIST_URL);

// 获取某一个spu的图片列表
export const reqSpuImageList = (spuId: number | string) =>
  request.get<any, SpuImageResponseData>(API.SPU_IMAGE_LIST_URL + spuId);

// 获取某一个spu的销售属性列表
export const rqeSpuSaleAttrList = (spuId: number | string) =>
  request.get<any, SpuSaleAttrResponseData>(API.SPU_SALE_ATTR_LIST_URL + spuId);

// 获取销售属性列表
export const reqBaseSaleAttrList = () =>
  request.get<any, BaseSaleAttrResponseData>(API.BASE_SALE_ATTR_LIST_URL);

// 添加或修改SPU spuInfo: SPU的基本数据类型
export const reqAddOrUpdateSpu = (spuInfo: SpuDetail) => {
  // 根据是否有id来判断是添加还是修改
  if (spuInfo.id) {
    // 修改
    return request.post<any, any>(API.UPDATE_SPU_INFO_URL, spuInfo);
  } else {
    // 添加
    return request.post<any, any>(API.SAVE_SPU_INFO_URL, spuInfo);
  }
};

// 保存SKU
export const reqSaveSkuInfo = (skuInfo: SkuData) =>
  request.post<any, any>(API.SAVE_SKU_INFO_URL, skuInfo);

// 根据spuId获取spu的基本信息
export const reqSkuInfo = (spuId: number | string) =>
  request.get<any, SKUResponseData>(API.SKU_INFO_URL + spuId);

// 根据spuId删除对应的spu
export const reqDeleteSku = (spuId: number | string) =>
  request.delete<any, any>(API.DELETE_SKU_URL + spuId);
